package com.warmlight.voicepacket.ui.user;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.flyco.tablayout.SlidingTabLayout;
import com.warmlight.voicepacket.R;
import com.warmlight.voicepacket.adapter.MyMessageVAdapter;
import com.warmlight.voicepacket.base.BaseActivity;
import com.warmlight.voicepacket.data.eventbus.Login;
import com.warmlight.voicepacket.utils.SPUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import io.rong.imkit.RongIM;
import io.rong.imkit.manager.IUnReadMessageObserver;
import io.rong.imlib.model.Conversation;

public class MyMessageActivity extends BaseActivity {

    private SlidingTabLayout stl_square;
    private ViewPager mViewPager;

    private MyMessageVAdapter mRemindAdapter;
    private TextView tv_tab_title;
    private IUnReadMessageObserver iUnReadMessageObserver;
    private LinearLayout ll_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mymessage);

        init();
        EventBus.getDefault().register(this);
    }

    public void init() {

        stl_square = (SlidingTabLayout) findViewById(R.id.stl_square);
        mViewPager = (ViewPager) findViewById(R.id.vp_square);
        ll_back = (LinearLayout) findViewById(R.id.ll_back);

        // 设置ViewPager属性
        mRemindAdapter = new MyMessageVAdapter(getSupportFragmentManager(), this);
        mViewPager.setOffscreenPageLimit(2);
        mViewPager.setAdapter(mRemindAdapter);



        // 定义 SlidingTabLayout
        stl_square.setViewPager(mViewPager); // 加载ViewPager
        iUnReadMessageObserver=new IUnReadMessageObserver() {//监听消息数
            @Override
            public void onCountChanged(int i) {
                if (i>0){
                    stl_square.showMsg(1,0);
                }else {
                    stl_square.hideMsg(1);
                }

            }
        };
        ll_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        RongIM.getInstance().addUnReadMessageCountChangedObserver(iUnReadMessageObserver, Conversation.ConversationType.PRIVATE);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void XXX(Login login){
        if (!SPUtils.isLogined()){
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RongIM.getInstance().removeUnReadMessageCountChangedObserver(iUnReadMessageObserver);
        EventBus.getDefault().unregister(this);
    }
}